<template>
  <div>
    <!-- 查询表单区域 -->
    <!-- 搜索框 -->
    <el-card class="box-card card-toubu" shadow="never">
      <div class="text item">
        <i class="el-icon-search"></i>&nbsp;
        <span>筛选搜索</span>
        <br />
        <br />
		<el-form :inline="true" :model="brand" class="demo-form-inline">
		  <el-form-item label="品牌编号">
			<el-input v-model="brand.id" placeholder="请输入品牌编号"></el-input>
		  </el-form-item>
		  <el-form-item label="品牌名称">
			<el-input v-model="brand.name" placeholder="请输入品牌名称"></el-input>
		  </el-form-item>
		  <el-form-item label="品牌首字母">
			<el-input
			  v-model="brand.letter"
			  placeholder="请输入品牌首字母"
			></el-input>
		  </el-form-item>
		  <el-form-item>
			<el-button type="primary" @click="onSubmit">查询</el-button>
			<!-- 添加品牌按钮 -->
			<el-button
			  type="primary"
			  @click="
				dialogFormVisible = true;
				addOrUpdate = '添加';
			  "
			  >新增品牌</el-button
			>
		  </el-form-item>
		</el-form>
	  </div>
	</el-card>
	<el-card class="box-card card-toubu" shadow="never">
	  <div class="text item">
	    <i class="el-icon-tickets"></i>&nbsp;
	    <span>数据列表</span>
	  </div>
	</el-card>
    <!-- 表格区域 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="品牌编号" width="280" prop="id"></el-table-column>
      <el-table-column
        label="品牌名称"
        width="280"
        prop="name"
      ></el-table-column>
      <el-table-column label="品牌图片" width="380">
        <template slot-scope="scope">
          <img :src="scope.row.image" width="200px" height="80px" />
        </template>
      </el-table-column>
      <el-table-column
        label="品牌首字母"
        width="280"
        prop="letter"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="
              handleEdit(scope.row.id);
              addOrUpdate = '编辑';
            "
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页控件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- dialog对话框控件 -->
    <el-dialog
      :title="addOrUpdate + '品牌'"
      :visible.sync="dialogFormVisible"
      @close="queryBrand = {}"
    >
      <el-form :model="queryBrand">
        <el-form-item label="品牌名称" :label-width="formLabelWidth">
          <el-input v-model="queryBrand.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:88/api/oss/file"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="首字母" :label-width="formLabelWidth">
          <el-input v-model="queryBrand.letter" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="queryBrand.seq" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelAddOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 数据接收数组
      tableData: [],
      // 总行数
      total: 0,
      // 选择显示每页多少数据数组
      pageSizes: [10, 20, 30, 40],
      // 页号
      currentPage: 1,
      // 页大小
      pageSize: 10,
      // 查询表单数据
      brand: {
        id: "",
        name: "",
        letter: ""
      },
      // 对话框展示，默认不展示
      dialogFormVisible: false,
      // 判断对话框是添加还是修改
      addOrUpdate: "",
      // dialog对话框输入框长度
      formLabelWidth: "120px",
      // dialog对话框数据
      queryBrand: {
        name: "",
        letter: "",
        seq: "",
        image: ""
      },
      // 图片展示路径
      imageUrl: ""
    };
  },
  methods: {
    // 获取数据的方法
    async getBrands() {
      const { data: res } = await this.$http.post(
        `vi/goods/brand/pageQueryByAll/${this.currentPage}/${this.pageSize}`,
        this.brand
      );
      this.tableData = res.data.pageUtils.rows;
      this.total = res.data.pageUtils.total;
    },
    // 通过Id查询品牌方法
    async handleEdit(id) {
      const { data: res } = await this.$http.get(
        `vi/goods/brand/detail/${id}`
      );
      this.queryBrand = res.data.tbBrand;
      this.dialogFormVisible = true;
    },
    // 通过Id删除品牌方法
    handleDelete(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.delete(`vi/goods/brand/delete/${id}`);
          this.$message.success("删除成功！");
          // 刷新页面
          this.getBrands();
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 页大小改变方法
    handleSizeChange(val) {
      this.pageSize = val;
      this.getBrands();
    },
    // 页号改变方法
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getBrands();
    },
    // 查询方法
    onSubmit() {
      this.getBrands();
    },
    // 添加或修改品牌方法
    async handelAddOrUpdate() {
      this.queryBrand.image = this.imageUrl;
	  if (this.queryBrand.id) {
		  const { data: res } = await this.$http.put(`vi/goods/brand/update`, this.queryBrand)
		  this.$message.success(res.message);
	  } else {
		  const { data: res } = await this.$http.put(`vi/goods/brand/add`, this.queryBrand)
		  this.$message.success(res.message);
	  }
      this.dialogFormVisible = false;
      this.getBrands();
    },
    // 上传成功的方法
    handleAvatarSuccess(file) {
      this.imageUrl = file.data.fileUrl;
    },
    // 上传之前的方法
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  },
  created() {
    this.getBrands();
  }
};
</script>

<style scoped="scoped">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
